/******************************************************************************
* Copyright (C) 2016, Huada Semiconductor Co.,Ltd All rights reserved.
*
* This software is owned and published by:
* Huada Semiconductor Co.,Ltd ("HDSC").
*
* BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
* BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
*
* This software contains source code for use with HDSC
* components. This software is licensed by HDSC to be adapted only
* for use in systems utilizing HDSC components. HDSC shall not be
* responsible for misuse or illegal use of this software for devices not
* supported herein. HDSC is providing this software "AS IS" and will
* not be responsible for issues arising from incorrect user implementation
* of the software.
*
* Disclaimer:
* HDSC MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
* REGARDING THE SOFTWARE (INCLUDING ANY ACOOMPANYING WRITTEN MATERIALS),
* ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
* WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
* WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
* WARRANTY OF NONINFRINGEMENT.
* HDSC SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
* NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
* LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
* LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
* INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
* SAVINGS OR PROFITS,
* EVEN IF Disclaimer HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
* YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
* INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
* FROM, THE SOFTWARE.
*
* This software may be replicated in part or whole for the licensed use,
* with the restriction that this Disclaimer and Copyright notice must be
* included with each copy of this software, whether used in part or whole,
* at all times.
*/
/******************************************************************************/
/** \file main.c
 **
 ** A detailed description is available at
 ** @link Sample Group Some description @endlink
 **
 **   - 2016-02-16  1.0  XYZ First version for Device Driver Library of Module.
 **
 ******************************************************************************/

/******************************************************************************
 * Include files
 ******************************************************************************/

#include "FreeRTOS.h"
#include "task.h"
#include "timers.h"
#include "queue.h"
#include "semphr.h"
#include "event_groups.h"
#include "FreeRTOSConfig.h"

#include "gpio.h"
/******************************************************************************
 * Local pre-processor symbols/macros ('#define')
 ******************************************************************************/

/******************************************************************************
 * Global variable definitions (declared in header file with 'extern')
 ******************************************************************************/

/******************************************************************************
 * Local type definitions ('typedef')
 ******************************************************************************/

/******************************************************************************
 * Local function prototypes ('static')
 ******************************************************************************/

/******************************************************************************
 * Local variable definitions ('static')                                                   *
 ******************************************************************************/

/******************************************************************************
 * Local pre-processor symbols/macros ('#define')
 ******************************************************************************/

/*****************************************************************************
 * Function implementation - global ('extern') and local ('static')
 ******************************************************************************/
static void Gpio_Config(void);
/*声明一个类型为 SemaphoreHandle_t  的变量. 其用于保存二值信号量的句柄，以便在任务中可以引用此队列*/
SemaphoreHandle_t  xBinarySemaphore; 
 
/*按键IO中断*/
void Gpio_IRQHandler(uint8_t u8Param)
{
    static portBASE_TYPE xHigherPriorityTaskWoken;
	if(3 == u8Param)
    {
		/*清除中断标志位*/
		Gpio_ClearIrq(GpioPortD, GpioPin4);
		
		xHigherPriorityTaskWoken = pdFALSE;
		
		/*给出一个信号量*/
		xSemaphoreGiveFromISR(xBinarySemaphore, &xHigherPriorityTaskWoken);
		
		/*等此信号量的任务解除阻塞，如果优先级高于当前任务，则强制进行一次任务切换*/
		portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
	}
}
 
void vTask1(void *pvParameters)
{	 
    while (1)
    {		
		/* 输出高电平，LED3灭 */
		Gpio_ClrIO(GpioPortD, GpioPin5);
		/* 延时500ms，延时期间任务进入阻塞态 */
		vTaskDelay(500/portTICK_RATE_MS);
		/* 输出低电平，LED3亮*/
		Gpio_SetIO(GpioPortD, GpioPin5);
		/* 延时500ms，延时期间任务进入阻塞态 */
		vTaskDelay(500/portTICK_RATE_MS); 

    }
}
 
 void vTask2(void *pvParameters)
 {	 
    while (1)
    {
		/*使用信号量等待一个事件，无超时阻塞，直到收到信号量才会接着往下执行*/
        xSemaphoreTake(xBinarySemaphore, portMAX_DELAY);
		/*收到信号量，关闭led指示灯*/
		Gpio_ClrIO(GpioPortD, GpioPin5);
		delay1ms(5000); //延时等待5s		
    }
 }
/**
 ******************************************************************************
 ** \brief  Main function of project
 **
 ** \return uint32_t return value, if needed
 **
 ** This sample
 **
 ******************************************************************************/
int32_t main(void)
{
	/* IO口配置 */
    Gpio_Config();
	
	/*创建一个二值信号量*/
	xBinarySemaphore = xSemaphoreCreateBinary();
	
	/*检查信号量是否成功创建*/
	if(xBinarySemaphore != NULL)
    {
		/*创建任务1，控制led灯闪烁，优先级1*/
		xTaskCreate(vTask1, "Task1", 100, NULL, 1, NULL);
		/*创建任务2，控制led灯熄灭固定时长，优先级2，优先级高于任务1*/
		xTaskCreate(vTask2, "Task2", 100, NULL, 2, NULL);
		/*启动调度器，任务开始执行*/
		vTaskStartScheduler();
    }else
	{
		; /*信号量创建失败*/
	}
	while(1);
}

/* CPU IO口配置（包含LED灯和按键的配置）*/
static void Gpio_Config(void)
{
    stc_gpio_config_t stcGpioCfg;
	
    Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE);  /* 打开GPIO外设时钟门控 */
    
    stcGpioCfg.enDir = GpioDirIn;                            /* 端口方向配置->输入 */    
    stcGpioCfg.enDrv = GpioDrvL;                             /* 端口驱动能力配置->高驱动能力 */
    stcGpioCfg.enPuPd = GpioPu;                              /* 端口上下拉配置->上拉 */
    stcGpioCfg.enOD = GpioOdDisable;                         /* 端口开漏输出配置->开漏输出关闭 */
    stcGpioCfg.enCtrlMode = GpioAHB;                         /* 端口输入/输出值寄存器总线控制模式配置->AHB */
    
    Gpio_Init(GpioPortD, GpioPin4, &stcGpioCfg);             /* GPIO IO PD04初始化(PD04在STK上外接KEY(USER)) */
    
    Gpio_ClearIrq(GpioPortD, GpioPin4);                      /* 清中断标志位 */
    Gpio_EnableIrq(GpioPortD, GpioPin4, GpioIrqRising);      /* 打开并配置PD04为上升沿中断 */
    EnableNvic(PORTD_IRQn, IrqLevel3, TRUE);                 /* 使能端口PORTD系统中断 */
	
    stcGpioCfg.enDir = GpioDirOut;                           /* 端口方向配置->输出(其它参数与以上（输入）配置参数一致) */
    stcGpioCfg.enPuPd = GpioPd;                              /* 端口上下拉配置->下拉 */

    Gpio_Init(GpioPortD, GpioPin5, &stcGpioCfg);             /* GPIO IO PD05初始化(PD05在STK上外接LED(LED3)) */
}

/******************************************************************************
 * EOF (not truncated)
 ******************************************************************************/


